package bairui.international.console.web.controller.index.module.image;

import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lianjing.sh.model.uml.model.status.State_UpDown;
import com.lianjing.sh.project.commons.util.Util_FileDeal;
import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.lianjing.sh.validate.spring.exception.ValidateObjectException;
import com.sh_lianjing.util.Util_BeanCopyByDeclaredProperties;
import com.sh_lianjing.web.http.mime.Util_Mime;

import bairui.international.model.domain.jpa.TB_IndexModule;
import bairui.international.model.domain.jpa.TB_IndexModuleImg;
import bairui.international.model.type.Type_IndexModuleImg;
import bairui.international.service.Service_IndexModuleImg;

/**
 *
 * 添加图片
 *
 * @generated
 * 
 * @see STest_C_Index_Module_Image_AddImage_FileUpload 测试
 *
 */
@Controller
public class C_Index_Module_Image_AddImage_FileUpload
 extends SC_Index_Module_Image_AddImage_FileUpload 
{
	
	@Autowired private Service_IndexModuleImg service_IndexModuleImg;
	
	/**
	 * 上传文件
	 */
	@Action(FileUpload.class)
	@PostMapping(FileUpload.RequestMappingValue)
	public @FomatOutput @ResponseBody Object fileUpload(
		@Valid Input_FileUpload_Input_Upload.Model input, 
		@PathVariable(name=FileUpload.PathVariableName.ModuleId) long moduleId
	) throws Exception {
		for(MultipartFile item : input.getFileUrlFile()){
			TB_IndexModuleImg bean = new TB_IndexModuleImg();
			// 判断格式
			if(null != ImageIO.read(item.getInputStream())){
				bean.setFormat(Type_IndexModuleImg.Img);
			}else if(isVideo(item.getInputStream())){
				bean.setFormat(Type_IndexModuleImg.Video);
			}else{
				throw new ValidateObjectException("object", "", "不是图片或视频格式");
			}
			// 上传文件相关
			Util_FileDeal
				.saveFile(item, pathConfig.getPosition_Image_IndexModuleImg())
				.resultTo(bean);
			// 保存数据
			TB_IndexModuleImg save = Util_BeanCopyByDeclaredProperties.copyProperties(input, bean);
				save.setIndexModule(new TB_IndexModule(moduleId));
				save.setState(State_UpDown.t_create().to());
			service_IndexModuleImg.save(save);
		}
		return null;
	}
	
	/**
	 * 判断视频格式
	 */
	boolean isVideo(InputStream inputStream) throws IOException{
		String f = Util_Mime.detectMimeType(inputStream);
		return f.contains("video/");
	}

//	public static void main(String[] args) throws IOException {
//		String fileName = "2ded247dc9914a0dc747262629eaa5ee.mp4";
//		String f = Util_Mime.detectExtension(C_Index_Module_Image_AddImage_FileUpload.class.getClassLoader().getResource(fileName));
//		System.out.println(f);
//	}

}
